package com.aaa.controller;

import com.aaa.dto.GoodsLoginInfo;
import com.aaa.dto.UpdateCheckGoodsLogin;
import com.aaa.dto.UpdateGoodsInfo;
import com.aaa.pojo.DefaultMsg;
import com.aaa.pojo.Goods;
import com.aaa.pojo.GoodsType;
import com.aaa.service.GoodsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/goods")
public class GoodsController {

    private final GoodsService goodsService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    /**
     * 获取所有商品
     *
     * @return 返回所有商品集合
     */
    @GetMapping("/findAllGoods")
    public List<Goods> findAllGoods() {
        return goodsService.findAllGoods();
    }

    /**
     * 获取所有商品类型集合
     *
     * @return 返回商品类型类型集合
     */
    @GetMapping("/findAllType")
    public List<GoodsType> findAllType() {
        return goodsService.findAllType();
    }

    /**
     * 添加商品
     *
     * @return 返回http信息 data中有是否成功的信息
     */
    @PostMapping("/addGoods")
    public DefaultMsg addGoods(Goods goods) {
        return goodsService.addGoods(goods);
    }

    /**
     * 新增商品图片上传
     *
     * @return 返回http信息 data中有是否成功的信息
     */
    @PostMapping("/addGoodsPicture")
    public DefaultMsg addGoodsPicture(MultipartFile file) {
        DefaultMsg defaultMsg = new DefaultMsg(200, 1, null, null);
        String filePath = "E:/project_3_vue2/picture/";
        String fileName = UUID.randomUUID().toString();
        File cloningFile = new File(filePath + fileName + ".jpg");
        try {
            file.transferTo(cloningFile);
            defaultMsg.setData(fileName);
        } catch (IOException e) {
            defaultMsg.setMsg(0);
            throw new RuntimeException(e);
        }
        return defaultMsg;
    }

    /**
     * 修改商品上下架
     *
     * @return 返回http信息 data中有是否成功的信息
     */
    @PostMapping("/updateStateOfGoodsById")
    public DefaultMsg updateStateOfGoodsById(Integer id, Integer state) {
        return goodsService.updateStateOfGoodsById(id, state);
    }

    /**
     * 商品入库（购入商品）
     *
     * @param id       商品id
     * @param buyCount 购入数量
     * @return 返回http信息
     */
    @PostMapping("/updateCountOfGoodsById")
    public DefaultMsg updateCountOfGoodsById(Integer id, Integer buyCount) {
        return goodsService.updateCountOfGoodsById(id, buyCount);
    }

    /**
     * 添加商品入库记录
     *
     * @param goodsLoginInfo 商品入库记录
     * @return
     */
    @PostMapping("/addGoodsLoginInfo")
    public DefaultMsg addGoodsLoginInfo(GoodsLoginInfo goodsLoginInfo) {
        return goodsService.addGoodsLoginInfo(goodsLoginInfo);
    }

    @GetMapping("/findGoodsLoginInfo")
    public List<GoodsLoginInfo> findGoodsLoginInfo() {
        return goodsService.findGoodsLoginInfo();
    }

    @PostMapping("/updateStatusById")
    public DefaultMsg updateStatusById(UpdateCheckGoodsLogin updateCheckGoodsLogin) {
        return goodsService.updateStatusById(updateCheckGoodsLogin);
    }

    @PostMapping("/updateGoodsInfoByGoodsId")
    public DefaultMsg updateGoodsInfoByGoodsId(UpdateGoodsInfo updateGoodsInfo) {
        return goodsService.updateGoodsInfoByGoodsId(updateGoodsInfo);
    }

}
